package com.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    public final static String YYYYMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public final static String YYYY_MM_dd_HH_mm_ss = "yyyyMMddHHmmss";
    public final static String YYYYMMdd = "yyyyMMdd";
    public final static String datePath = "yyyy/MM/dd";
    public final static String tiggerDate = "yy-MM-dd";
    public final static String YYYY_MM_dd = "yyyy-MM-dd";
    public final static String YYYYMMddHHmmssSSS = "yyyy-MM-dd HH:mm:ss:SSS";
    public final static String YYYY_MM_dd_HH = "yyyy-MM-dd HH";

    public static Date getNowDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date now = calendar.getTime();
        return now;
    }

    /**
     * 将不规范的String日期转成规范的String日期
     *
     * @param strDate
     * @return
     */
    public static String formatStringDate(String strDate) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (null != strDate) {
            try {
                Date getDate = sdf.parse(strDate);
                String formatDate = sdf.format(getDate);
                return formatDate;
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            }
        } else {
            return null;
        }

    }

    /**
     * 日期格式转换
     *
     * @param date
     * @return
     */
    public static String format(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        try {
            return sdf.format(date);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取自定义的时间类型yyyy-MM-dd 24:59:59 最大时间
     *
     * @return
     * @author chenby
     * @date 2017-9-25
     * @function
     */
    public static Date parseDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd 24:59:59");
        Date dates = null;
        try {
            dates = sdf.parse(f.format(date));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return dates;

    }


    /**
     * 将格式为：2017-03-28 15:49:48的时间
     * 转化为：3月28日 星期二 15:49
     *
     * @param strDate
     * @return
     */
    public static String getInterviewDate(String strDate) {
        //获得星期几
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("M月dd日 E HH:mm");try {
            //将String的日期转为Date的日期
            Date getDate = sdf.parse(strDate);
            //x月xx日
            String format1 = sdf1.format(getDate);
            //处理时和分钟
            String returnDate = format1;
            return returnDate;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 获取系统时间 String 类型 （默认格式yyyy-MM-dd HH:mm:ss）
     *
     * @return
     */
    public static String getSystemTimeStr() {
        return getSystemTimeStr(ConstantDefine.YMD_HMS);
    }

    /**
     * 获取系统时间 String 类型 （自定义格式）
     *
     * @param format
     * @return
     */
    public static String getSystemTimeStr(String format) {
        return getFormatTime(format, new Date());
    }

    /**
     * 获取自定义格式的指定时间
     *
     * @param format 默认yyyy-MM-dd HH:mm:ss格式
     * @param date
     * @return
     */
    public static String getFormatTime(String format, Date date) {
        if (date == null) {
            date = new Date();
        }
        if (StringUtil.isEmpty(format)) {
            format = ConstantDefine.YMD_HMS;
        }
        SimpleDateFormat f = new SimpleDateFormat(format);
        return f.format(date);
    }

    /**
     * 将日期字符串转化为日期。失败返回null。
     *
     * @param date     日期字符串
     * @param parttern 日期格式
     * @return 日期
     */
    public static Date StringToDate(String date, String parttern) {
        Date myDate = null;
        if (date != null) {
            try {
                myDate = getDateFormat(parttern).parse(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return myDate;
    }

    /**
     * 将日期转化为日期字符串。失败返回null。
     *
     * @param date     日期
     * @param parttern 日期格式
     * @return 日期字符串
     */
    public static String DateToString(Date date, String parttern) {
        String dateString = null;
        if (date != null) {
            try {
                dateString = getDateFormat(parttern).format(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return dateString;
    }

    /**
     * 获取SimpleDateFormat
     *
     * @param parttern 日期格式
     * @return SimpleDateFormat对象
     * @throws RuntimeException 异常：非法日期格式
     */
    private static SimpleDateFormat getDateFormat(String parttern) throws RuntimeException {
        return new SimpleDateFormat(parttern);
    }

    /**
     * 计算两个日期间隔的天数
     *
     * @param beginDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static int daysBetween(Date beginDate, Date endDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(ConstantDefine.YMD);
        beginDate = sdf.parse(sdf.format(beginDate));
        endDate = sdf.parse(sdf.format(endDate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(beginDate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(endDate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 计算两个日期间隔的天数
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static int daysBetween(String beginDate, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat(ConstantDefine.YMD);
        int daysBetween = 0;
        try {
            Date dateBefore = sdf.parse(beginDate);
            Date dateAfter = sdf.parse(endDate);
            daysBetween = daysBetween(dateBefore, dateAfter);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return daysBetween;
    }

    /**
     * 计算两个日期间隔的分钟数
     *
     * @param before
     * @param after
     * @return
     */
    public static int minutesBetween(String before, String after) {
        Date dateBefore = DateUtil.StringToDate(before, ConstantDefine.YMD_HMS);
        Date dateAfter = DateUtil.StringToDate(after, ConstantDefine.YMD_HMS);
        long time1 = dateBefore.getTime();
        long time2 = dateAfter.getTime();
        long timesBetween = (time2 - time1) / 1000 / 60;
        return Integer.parseInt(String.valueOf(timesBetween));
    }

    /**
     * 两个时间相差距离多少秒
     *
     * @param
     * @param
     * @return String 返回值为：xx天xx小时xx分xx秒
     */
    public static long getDistanceTime(String starttime, String endtime) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            one = df.parse(starttime);
            two = df.parse(endtime);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sec;
    }

    /**
     * 判断某一天是星期几
     *
     * @param dt
     * @return
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    /**
     * 判断某一天是星期几
     *
     * @param strDate
     * @return
     */
    public static String getWeekOfDate(String strDate) {
        Date dt = StringToDate(strDate, ConstantDefine.YMD);
        return getWeekOfDate(dt);
    }

    /**
     * 判断一个时间是否为下午
     *
     * @param strDate
     * @return
     * @throws ParseException
     */
    public static boolean isAfternoon(String strDate) {
        boolean flag = false;
        String defaultTime = "12:00";
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Date dat;
        Date datDefault;
        try {
            dat = sdf.parse(strDate);
            datDefault = sdf.parse(defaultTime);
            if (dat.getTime() > datDefault.getTime()) {
                flag = true;
            }
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return flag;
    }

    /**
     * 将字符串转化成日期格式：20161005 ---> 2016-10-05
     *
     * @param str
     * @return
     */
    public static String formatStringToYmd(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat(ConstantDefine.YMD);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        String result = null;
        try {
            result = sdf.format(sdf2.parse(str));
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return result;
    }

    /**
     * 求两个时间相差的小时数
     *
     * @param time1
     * @param time2
     * @return
     */
    public static int hoursBetween(String time1, String time2) {
        SimpleDateFormat sdf = new SimpleDateFormat(ConstantDefine.HMS);
        long hours = 0;
        try {
            Date date1 = sdf.parse(time1);
            Date date2 = sdf.parse(time2);
            long millis = date2.getTime() - date1.getTime();
            long minutes = millis / (1000 * 60);
            if (minutes % 60 == 0) {
                hours = minutes / 60;
            } else {
                long hourRemain = minutes % 60;
                hours = (minutes - hourRemain) / 60 + 1;
            }
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return (int) hours;
    }

    /**
     * 获取年月日：2016-10-05 11:27:25.0 ---> 2016-10-05
     *
     * @param strDate
     * @return
     */
    public static String getYMDfromDate(String strDate) {
        String result = strDate.substring(0, 10);
        return result;
    }

    /**
     * 获取上个月或者这个月的第一天
     *
     * @return
     */
    public static String getFirstDay(int state) {
        Calendar calendar = Calendar.getInstance();
        if (state == 1) {
            calendar.add(Calendar.MONTH, -1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
        } else if (state == 0) {
            calendar.set(Calendar.DAY_OF_MONTH, 1);
        }
        SimpleDateFormat sdf = new SimpleDateFormat(ConstantDefine.YMD);
        return sdf.format(calendar.getTime());
    }

    /**
     * 将字符串转化为时间 HH:mm:ss
     *
     * @param times
     * @return
     */
    public static Date formateStringToTime(String times) {
        Date myTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat(ConstantDefine.HMS);
        if (times != null) {
            try {
                myTime = sdf.parse(times);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return myTime;
    }

    /**
     * 比较传入的时间格式字符串与当前时间
     * 大于当前日期     = 1,
     * 小于当前日期     = -1,
     * 等于当前日期     = 0
     *
     * @param strDateTime 传入的时间字符串
     * @return
     * @throws StringIndexOutOfBoundsException
     * @author leixin
     * @createTime 下午12:46:26
     */
    public static int compareWithNow(String strDateTime) throws StringIndexOutOfBoundsException {
        String now = getSystemTimeStr();
        //截取年份
        String sysYear = now.substring(0, 4);
        String year = strDateTime.substring(0, 4);
        //截取月份
        String sysMonth = now.substring(5, 7);
        String month = strDateTime.substring(5, 7);
        //截取日
        String sysDay = now.substring(8, 10);
        String day = now.substring(8, 10);
        //判断年份
        if (Integer.parseInt(sysYear) > Integer.parseInt(year)) {
            return -1;
        } else if (Integer.parseInt(sysYear) < Integer.parseInt(year)) {
            return 1;
        } else {
            //判断月份
            if (Integer.parseInt(sysMonth) > Integer.parseInt(month)) {
                return -1;
            } else if (Integer.parseInt(sysMonth) < Integer.parseInt(month)) {
                return 1;
            } else {
                //判断日期
                if (Integer.parseInt(sysDay) > Integer.parseInt(day)) {
                    return -1;
                } else if (Integer.parseInt(sysDay) < Integer.parseInt(day)) {
                    return 1;
                }
            }
        }
        return 0;
    }

    /**
     * 获取当前时间：格式YYYY-MM-dd HH:mm:ss
     *
     * @return
     * @author wuronglai
     */
    public static String getNow() {
        return new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date());
    }

    /**
     * 获取未来 第 past 天的日期
     *
     * @param past
     * @return
     * @author chenby
     * @date 2017-9-16
     * @function
     */
    public static String getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String result = format.format(today);
        return result;
    }

    /**
     * 根据原来的时间（Date）获得相对偏移 N 天的时间（Date）
     *
     * @param protoDate               原来的时间（java.util.Date）
     * @param dateOffset（向前移正数，向后移负数）
     * @param type                    指定不同的格式（1：年月日，2：年月日时，3：年月日时分）
     * @return 时间（java.util.Date），没有时分秒
     */
    public static Date getOffsetSimpleDate(Date protoDate, int dateOffset, int type) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(protoDate);
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - dateOffset);
        if (type == 1) {
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
        }
        if (type == 2) {
            cal.set(Calendar.MINUTE, 0);
        }
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public static Date addSecond(String strDate, int seconds) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date getDate = sdf.parse(strDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDate);
        calendar.add(Calendar.SECOND, seconds);
        return calendar.getTime();
    }

    public static String addMinute(String strDate, int minute) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date getDate = sdf.parse(strDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDate);
        calendar.add(Calendar.MINUTE, minute);
        return sdf.format(calendar.getTime());
    }

    public static String addHour(String strDate, int hour) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date getDate = sdf.parse(strDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDate);
        calendar.add(Calendar.HOUR, hour);
        return sdf.format(calendar.getTime());
    }


    public static String addDay(String strDate, int day) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date getDate = sdf.parse(strDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDate);
        calendar.add(Calendar.DAY_OF_WEEK, day);
        return sdf.format(calendar.getTime());
    }

    public static Date addDayByForm(String strDate, String from, int day) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(from);
        Date getDate = sdf.parse(strDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDate);
        calendar.add(Calendar.DAY_OF_WEEK, day);
        return calendar.getTime();
    }


    public static String resultYearDay(Date strDate, int day) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date getDate = strDate;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDate);
        calendar.add(Calendar.DAY_OF_WEEK, day);
        return sdf.format(calendar.getTime());
    }


    /**
     * 传入date类型，指定加减天数返回date类型
     *
     * @param strDate
     * @param day
     * @return
     */
    public static Date addDayToDate(Date strDate, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(strDate);
        c.add(Calendar.DAY_OF_MONTH, day);
        return c.getTime();
    }

    /**
     * 传入date类型，指定加减小时返回date类型
     *
     * @param strDate
     * @param hour
     * @return
     */
    public static Date addHourToDate(Date strDate, int hour) {
        Calendar c = Calendar.getInstance();
        c.setTime(strDate);
        c.set(Calendar.HOUR, c.get(Calendar.HOUR) + hour);// 让日期加1
        return c.getTime();
    }

    /**
     * 传入date类型，获取当前小时数
     *
     * @param strDate
     * @return
     */
    public static int getHourToDate(Date strDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(strDate);
        return c.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 传入date类型，获取当前月值
     *
     * @param strDate
     * @return
     */
    public static int getMonthToDate(Date strDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(strDate);
        return c.get(Calendar.MONTH) + 1;
    }

    /**
     * 传入date类型，获取当前年值
     *
     * @param strDate
     * @return
     */
    public static int getYearToDate(Date strDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(strDate);
        return c.get(Calendar.YEAR);
    }

    /**
     * 传入date类型，获取当前年值
     *
     * @param strDate
     * @return
     */
    public static String getYearMonthDayToDate(Date strDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(strDate);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DATE);
        return year + "/" + month + "/" + day;
    }

    /**
     * 传入date类型，获取当前日值
     *
     * @param strDate
     * @return
     */
    public static int getDayToDate(Date strDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(strDate);
        return c.get(Calendar.DATE);
    }

    /**
     * 获取时间精度到小时
     *
     * @return返回长时间格式 yyyy-MM-dd HH:mm:ss
     */
    public static Date getHourDay(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
        String dateString = formatter.format(date);
        ParsePosition pos = new ParsePosition(0);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }

    /**
     * 获取时间返回
     *
     * @return返回长时间格式 yyyy-MM-dd 00:00:00
     */
    public static Date getDayZero(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String dateString = formatter.format(date);
        ParsePosition pos = new ParsePosition(0);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }

    /**
     * 获取时间返回
     *
     * @return返回长时间格式 yyyy-MM-dd HH:00:00
     */
    public static Date getHourZero(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
        String dateString = formatter.format(date);
        ParsePosition pos = new ParsePosition(0);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }


    /**
     * 传入date类型，指定加减天数返回date类型
     *
     * @param strDate
     * @param day
     * @return
     */
    public static Date addDayToDay(Date strDate, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(strDate);
        c.add(Calendar.DAY_OF_MONTH, day);
        strDate = c.getTime();
        return strDate;
    }


    /**
     * 传入date类型，指定加减天数返回String类型
     *
     * @param strDate
     * @param day
     * @return
     */
    public static String addDayToString(Date strDate, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(strDate);
        c.add(Calendar.DAY_OF_MONTH, day);
        strDate = c.getTime();
        String trueDay = new SimpleDateFormat("yyyy-MM-dd").format(strDate);
        return trueDay;
    }

    /**
     * 获取当前时间：格式YYYY-MM-dd HH:mm:ss
     *
     * @return
     * @author wuronglai
     */
    public static String getNowForMat(String format) {
        return new SimpleDateFormat(format).format(new Date());
    }


    public static String modifyDate(String str, int day) {
        Date date;
        try {
            date = new SimpleDateFormat(DateUtil.YYYYMMdd).parse(str);//当前日期
            SimpleDateFormat format = new SimpleDateFormat(DateUtil.YYYYMMdd);//格式化对象
            Calendar calendar = Calendar.getInstance();//日历对象
            calendar.setTime(date);//设置当前日期
            calendar.add(Calendar.DAY_OF_MONTH, day);//加、减
            return format.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 将字符串转化成日期格式：
     *
     * @param format
     * @param dataString
     * @return
     */

    public static String formatString(String format, String dataString) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        SimpleDateFormat sdf2 = new SimpleDateFormat(DateUtil.YYYYMMddHHmmssSSS);
        String result = null;
        try {
            result = sdf.format(sdf2.parse(dataString));
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return result;
    }


    /**
     * java获取当前月每天的日期
     *
     * @return
     */
    public static List<Map<String, Object>> getDayListOfMonth(Integer day) throws Exception {
        List list = new ArrayList();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date s = new Date(System.currentTimeMillis()); //当前时间作为结束时间
        Calendar begin = Calendar.getInstance();
        Calendar begin1 = Calendar.getInstance();
        begin.setTime(s);
        begin.add(Calendar.DAY_OF_WEEK, day);//获取起始日期
        begin1.setTime(s);
        begin1.add(Calendar.DAY_OF_MONTH, 1);//获取起始日期
        Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(df.format(begin.getTime()));//定义起始日期
        Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse(df.format(begin1.getTime()));//定义结束日期
        Calendar dd = Calendar.getInstance();//定义日期实例
        dd.setTime(d1);//设置日期起始时间
        while (dd.getTime().before(d2)) {//判断是否到结束日期
            Map list1 = new HashMap();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String str = sdf.format(dd.getTime());
            list1.put("startDate", str);
            list.add(list1);
            dd.add(Calendar.DAY_OF_MONTH, 1);//进行当前日期月份加1
        }
        System.out.println(list);
        return list;
    }


    /**
     * 获取这个月的最大天数
     *
     * @param date
     * @return
     */
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public static String secondToHour(Integer num) {
        int h = num / 3600;
        int m = num % 3600 / 60;
        int s = num % 60; //不足60的就是秒，够60就是分
        String result = h + "小时" + m + "分" + s + "秒";
        return result;
    }

    /**
     * 时间格式转换
     *
     * @param date
     * @param format
     * @return
     */
    public static Date getNowDateShort(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(date);
        ParsePosition pos = new ParsePosition(0);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }


    /**
     * 时间格式转换
     *
     * @param date
     * @param format
     * @return
     */
    public static String getDateToString(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * 时间格式转换
     *
     * @param stringDate
     * @param format
     * @return
     */
    public static String getDateToString(String stringDate, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(stringDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * 判断日期是否是周末
     *
     * @param bDate
     * @return
     * @throws ParseException
     */
    public static boolean isWeekend(String bDate) throws ParseException {
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date bdate = format1.parse(bDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(bdate);
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 传入date类型，指定加减小时返回date类型
     *
     * @param strDate
     * @param minute
     * @return
     */
    public static Date addMinuteToDate(Date strDate, int minute) {
        Calendar c = Calendar.getInstance();
        c.setTime(strDate);
        c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minute);
        return c.getTime();
    }

    /**
     * 获取时间返回
     *
     * @return返回任意指定长时间格式
     */
    public static Date getDayFormat(Date date,SimpleDateFormat formatter) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        Date parse = null;
        try{
            parse = sdf.parse(dateString);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 获取指定时间当前月的第一天
     * @param date
     * @return
     */
    public static Date getMonthFirstday(Date date){
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return cale.getTime();
    }

    /**
     * 获取指定时间当前月的最后一天
     * @param date
     * @return
     */
    public static Date getMonthLastday(Date date){
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return cale.getTime();
    }

}
